package PIPL::ConfigurationTest;

use strict;

use base 'Test::Unit::TestCase';

use PIPL::Configuration;

use File::Spec;
use PIPL::System::Path;

my ($file, $lock_file, $default_data);

sub new {
    my $self = shift()->SUPER::new(@_);
    return $self;
}

sub set_up {
    $file = File::Spec->catfile($PIPL::System::Path::Self->tmpdir, sprintf('config-%s-%s.yml', time, $$));
    $lock_file = $file . '.lock';
    $default_data = { 'key' => 'value' };
}

sub tear_down {
    unlink $file if $file and -f $file;
    unlink $lock_file if $lock_file and -f $lock_file;
}

sub test_PIPL_Configuration_isa {
    my $self = shift;
    my $obj = PIPL::Configuration->new(file=>$file, default_data=>$default_data);
    $self->assert_not_null($obj);
    $self->assert($obj->isa('PIPL::Configuration'));
}

sub test_PIPL_Configuration_field_file {
    my $self = shift;
    my $obj = PIPL::Configuration->new(file=>$file, default_data=>$default_data);
    $self->assert_equals($file, $obj->{file});
}

sub test_PIPL_Configuration_field_data {
    my $self = shift;
    my $obj = PIPL::Configuration->new(file=>$file, default_data=>$default_data);
    unlink $file;
    $obj->load;
    $self->assert_equals('value', $obj->{data}->{key});
}

sub test_PIPL_Configuration_save {
    my $self = shift;
    unlink $file;
    my $obj = PIPL::Configuration->new(file=>$file, default_data=>$default_data);
    $obj->load;
    my $data = $obj->{data};
    $self->assert(not -f $file);
    $obj->save;
    $self->assert(-f $file);
    $obj->load;
    $self->assert_not_equals($data, $obj->{data});
    $self->assert_deep_equals($data, $obj->{data});
}

sub test_PIPL_Configuration_lock {
    my $self = shift;
    my $obj = PIPL::Configuration->new(file=>$file, default_data=>$default_data);
    unlink $lock_file;
    $self->assert(not -f $lock_file);
    $obj->lock;
    $self->assert(-f $lock_file);
    $obj->unlock;
    $self->assert(not -f $lock_file);
}

1;
